package com.cskaoyan.controller;
/*
 * @author 魏骏杰
 */

import com.cskaoyan.bean.User;
import com.cskaoyan.bean.basebean.BaseRespVo;
import com.cskaoyan.bean.mini_program_coupon.CouponBO;
import com.cskaoyan.bean.mini_program_coupon.CouponVO;
import com.cskaoyan.bean.zhao_order_bean.BasePageData;
import com.cskaoyan.service.MiniProgramCouponService;
import com.cskaoyan.utils.MarketPageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("wx/coupon")
public class MiniProgramCouponController {
    @Autowired
    MiniProgramCouponService couponService;

    @RequestMapping("mylist")
    public BaseRespVo mylist(CouponBO couponBO) {
        MarketPageUtil.startPage(couponBO);
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        User currentUser = (User) subject.getPrincipal();
        String name = currentUser.getUsername();

        couponBO.setName(name);

        //查询当前用户的优惠券列表
        List<CouponVO> list = couponService.selectCouponListByUsername(couponBO);

        //分页
        BasePageData<CouponVO> couponListPage = BasePageData.ok(list);
        return BaseRespVo.ok(couponListPage);
    }

    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody CouponBO couponBO) {
        switch (couponService.exchangeNewCoupon(couponBO.getCode())) {
            case 1:
                return BaseRespVo.ok();
            case -1:
                return BaseRespVo.fail("兑换码不存在");
            case -2:
                return BaseRespVo.fail("无法获得更多");
            default:
                return BaseRespVo.fail("未知错误！");
        }
    }

    @RequestMapping("list")
    public BaseRespVo list(CouponBO couponBO) {
        MarketPageUtil.startPage(couponBO);
        List<CouponVO> list = couponService.findAllAvailableCouponsList(couponBO);
        BasePageData<CouponVO> couponListPage = BasePageData.ok(list);
        return BaseRespVo.ok(couponListPage);
    }

    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody CouponBO couponBO) {
        switch (couponService.receiveCouponByCouponId(couponBO)) {
            case 1:
                return BaseRespVo.ok();
            case -1:
                return BaseRespVo.fail("未满足领取条件");
            case -2:
                return BaseRespVo.fail("无法获得更多");
            case -3:
                return BaseRespVo.fail("仅支持兑换");
            default:
                return BaseRespVo.fail("未知错误");
        }
    }

    @RequestMapping("selectlist")
    public BaseRespVo selectList(CouponBO couponBO) {
        List<CouponVO> list = couponService.selectCouponListWhichCanBeAllowedUse(couponBO);
        BasePageData<CouponVO> couponListPage = BasePageData.ok(list);
        return BaseRespVo.ok(couponListPage);
    }
}
